﻿#region Imports
using System;
using System.Drawing;
#endregion

namespace CheckerWin.Utils
{
    public static class Utilities
    {
        #region Declarations
        public const ContentAlignment left = ContentAlignment.MiddleLeft;
        public const ContentAlignment right = ContentAlignment.MiddleRight;
        #endregion

        #region Methods
        public static bool IsBoolean(object value)
        {
            bool res = false;
            return Boolean.TryParse(System.Convert.ToString(value), out res);
        }

        public static bool IsContentAlignment(object value)
        {
            try
            {
                ContentAlignment align = (ContentAlignment)value;
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static ContentAlignment GetOnlyLR(ContentAlignment align, ContentAlignment defaultAlign)
        {
            if (defaultAlign != left && defaultAlign != right) defaultAlign = right; // method default
            ContentAlignment? finalAlign = null;

            if (IsContentAlignment(align))
            {
                switch (align)
                {
                    case ContentAlignment.BottomLeft:
                    case ContentAlignment.MiddleLeft:
                    case ContentAlignment.TopLeft:
                        finalAlign = left;
                        break;

                    case ContentAlignment.BottomRight:
                    case ContentAlignment.MiddleRight:
                    case ContentAlignment.TopRight:
                        finalAlign = right;
                        break;

                    default:
                        finalAlign = defaultAlign;
                        break;
                }
            }

            return finalAlign.Value;
        }


        public enum eInversionMode
        {
            Horizontal = 1,
            Vertical = 2
        }

        public static ContentAlignment InvertAlignment(ContentAlignment cont, ContentAlignment? defaultAlignment = null, eInversionMode? mode = null)
        {
            if (BulkRenamer.Util.Utilities.IsNull(defaultAlignment)) defaultAlignment = ContentAlignment.MiddleLeft;
            if (BulkRenamer.Util.Utilities.IsNull(mode)) mode = eInversionMode.Horizontal;
            ContentAlignment? target = null;

            if (mode == eInversionMode.Horizontal)
            {
                if (defaultAlignment.ToString().EndsWith("Center"))
                    defaultAlignment = BulkRenamer.Util.Utilities.GetEnumFromText<ContentAlignment>(
                        defaultAlignment.ToString().Replace("Center", "Left"));

                if (cont.ToString().Contains("Left"))
                    target = BulkRenamer.Util.Utilities.GetEnumFromText<ContentAlignment>(cont.ToString().Replace("Left", "Right"));
                else if (cont.ToString().Contains("Right"))
                    target = BulkRenamer.Util.Utilities.GetEnumFromText<ContentAlignment>(cont.ToString().Replace("Right", "Left"));
            }
            else if (mode == eInversionMode.Vertical)
            {
                if (cont.ToString().StartsWith("Bottom"))
                    target = BulkRenamer.Util.Utilities.GetEnumFromText<ContentAlignment>(cont.ToString().Replace("Bottom", "Top"));
                else if (cont.ToString().Contains("Top"))
                    target = BulkRenamer.Util.Utilities.GetEnumFromText<ContentAlignment>(cont.ToString().Replace("Top", "Bottom"));
            }

            return !BulkRenamer.Util.Utilities.IsNull(target) && target.HasValue
                ? target.Value : defaultAlignment.Value;
        }
        #endregion
    }
}
